//
//  MBHomeMessageView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBHomeMessageView.h"
#import "MBHomeMessageModel.h"
#import "MBHomeItemView.h"

@interface MBHomeMessageView ()
@property (nonatomic,strong) NSMutableArray* itemArray;

@end

@implementation MBHomeMessageView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UILabel* titleLabel = [[UILabel alloc]init];
        self.titleLabel = titleLabel;
        titleLabel.text = @"今日店铺概况";
        titleLabel.font = [UIFont lf_mediumFontOfSize:16];
        titleLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [self addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset([UIView lf_sizeFromIphone6:18]);
            make.centerX.equalTo(self);
        }];
        
        MBHomeMessageModel* model1 = [[MBHomeMessageModel alloc]initWithIconName:@"home_shop_icon" andNameStr:@"店铺浏览量" andNumStr:@"256"];
        MBHomeMessageModel* model2 = [[MBHomeMessageModel alloc]initWithIconName:@"home_goods_see" andNameStr:@"商品浏览量" andNumStr:@"256"];

        MBHomeMessageModel* model3 = [[MBHomeMessageModel alloc]initWithIconName:@"home_all_money" andNameStr:@"销售总额" andNumStr:@"2560.12"];
        MBHomeMessageModel* model4 = [[MBHomeMessageModel alloc]initWithIconName:@"home_order_num" andNameStr:@"订单数" andNumStr:@"256"];
        MBHomeMessageModel* model5 = [[MBHomeMessageModel alloc]initWithIconName:@"home_order_one" andNameStr:@"客单价" andNumStr:@"256.02"];

        NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithObjects:@[@[model1,model2],@[model3,model4,model5]] forKeys:@[@"基本数据",@"交易数据"]];
        
        UIView* lastView = titleLabel;
        
        for (int i=0; i<dic.allKeys.count; i++) {
            NSString* key;
            if (i == 0) {
                key = @"基本数据";
            }else {
                key = @"交易数据";
            }
            NSArray* arrray = [dic objectForKey:key];

            UILabel* secLabel = [[UILabel alloc]init];
            secLabel.text = key;
            secLabel.font = [UIFont lf_mediumFontOfSize:12];
            secLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
            [self addSubview:secLabel];
            
            [secLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom).offset([UIView lf_sizeFromIphone6:12]);
                make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:8]);
            }];

            lastView = secLabel;
            CGFloat width = (SCREEN_WIDTH -[UIView lf_sizeFromIphone6:44])/2;
            for (int j=0; j<arrray.count; j++) {
                MBHomeMessageModel* model = arrray[j];
                MBHomeItemView* itemView = [[MBHomeItemView alloc]init];
                itemView.iconImageView.image = [UIImage imageNamed:model.iconName];
                itemView.nameLabel.text = model.titleStr;
                itemView.numLabel.text = model.numStr;
                [self.itemArray addObject:itemView];
                [self addSubview:itemView];
                
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lastView.mas_bottom).offset([UIView lf_sizeFromIphone6:22]+([UIView lf_sizeFromIphone6:26+45]*(j/2)));
                    make.left.equalTo(lastView).offset([UIView lf_sizeFromIphone6:11]+width*(j%2));
                    make.width.mas_equalTo(width);
                    make.height.mas_equalTo([UIView lf_sizeFromIphone6:45]);
                }];
                
                if (j == arrray.count -1) {
                    lastView = itemView;
                }
            }
            
            UIView* line = [[UIView alloc]init];
            line.backgroundColor = COLOR_BG;
            [self addSubview:line];
            
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom).offset([UIView lf_sizeFromIphone6:20]);
                make.left.right.equalTo(self);
                if (i == 0) {
                    make.height.mas_equalTo([UIView lf_sizeFromIphone6:1]);
                }else {
                    make.height.mas_equalTo([UIView lf_sizeFromIphone6:10]);
                }
            }];
            
            lastView = line;

        }

    }
    return self;
}

-(NSMutableArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(void)setViewModel:(MBHomeViewModel *)viewModel
{
    _viewModel = viewModel;
    
    @weakify(self);
    [RACObserve(self.viewModel, dataModel) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        for (int i=0; i<self.itemArray.count; i++) {
            MBHomeItemView* itemView = self.itemArray[i];
            
            if ([self.titleLabel.text isEqualToString:@"历史统计"]) {
                if (i == 0) {
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.store_num];
                }else if (i == 1){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.goods_num];
                }else if (i == 2){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%.2f",viewModel.dataModel.money];
                }else if (i == 3){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.order];
                }else {
                    itemView.numLabel.text = [NSString stringWithFormat:@"%.2f",viewModel.dataModel.price];
                }
            }else {
                if (i == 0) {
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.store_day];
                }else if (i == 1){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.goods_day];
                }else if (i == 2){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%.2f",viewModel.dataModel.order_money];
                }else if (i == 3){
                    itemView.numLabel.text = [NSString stringWithFormat:@"%ld",(long)viewModel.dataModel.order_day];
                }else {
                    itemView.numLabel.text = [NSString stringWithFormat:@"%.2f",viewModel.dataModel.day_price];
                }
            }
        }
    }];
}

@end
